{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "import warnings\n",
    "warnings.filterwarnings(\"ignore\")\n",
    "my = pd.read_csv('maoyanfilms_clean.csv')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "my = my.drop(['Unnamed: 0','films_id','films_name','films_comment_num','films_director','films_actor','films_introduction','films_comment'],axis=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>films_type</th>\n",
       "      <th>films_area</th>\n",
       "      <th>films_duration</th>\n",
       "      <th>film_first_time</th>\n",
       "      <th>films_score</th>\n",
       "      <th>films_box_office</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>喜剧 , 爱情</td>\n",
       "      <td>中国大陆</td>\n",
       "      <td>122分钟</td>\n",
       "      <td>2010-12-22</td>\n",
       "      <td>7.4</td>\n",
       "      <td>47200.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>爱情</td>\n",
       "      <td>中国香港</td>\n",
       "      <td>117分钟</td>\n",
       "      <td>2009-01-26</td>\n",
       "      <td>6.1</td>\n",
       "      <td>11299.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>爱情 , 剧情</td>\n",
       "      <td>中国台湾,中国大陆</td>\n",
       "      <td>128分钟</td>\n",
       "      <td>2012-02-14</td>\n",
       "      <td>7.8</td>\n",
       "      <td>13700.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>爱情</td>\n",
       "      <td>中国大陆,中国香港</td>\n",
       "      <td>112分钟</td>\n",
       "      <td>2012-02-09</td>\n",
       "      <td>7.1</td>\n",
       "      <td>2137.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>爱情 , 音乐 , 奇幻</td>\n",
       "      <td>中国香港,中国台湾</td>\n",
       "      <td>101分钟</td>\n",
       "      <td>2007-07-31</td>\n",
       "      <td>8.5</td>\n",
       "      <td>33.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>15425</th>\n",
       "      <td>NaN</td>\n",
       "      <td>中国香港</td>\n",
       "      <td>13分钟</td>\n",
       "      <td>2015-12</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>15426</th>\n",
       "      <td>剧情 , 短片</td>\n",
       "      <td>中国大陆</td>\n",
       "      <td>20分钟</td>\n",
       "      <td>2019-06-25</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>15427</th>\n",
       "      <td>纪录片 , 短片</td>\n",
       "      <td>中国大陆</td>\n",
       "      <td>4分钟</td>\n",
       "      <td>2016</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>15428</th>\n",
       "      <td>喜剧</td>\n",
       "      <td>中国香港</td>\n",
       "      <td>125分钟</td>\n",
       "      <td>2012-10-11</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>15429</th>\n",
       "      <td>纪录片</td>\n",
       "      <td>中国台湾</td>\n",
       "      <td>61分钟</td>\n",
       "      <td>2018-10-25</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>15430 rows × 6 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "           films_type films_area films_duration film_first_time  films_score  \\\n",
       "0            喜剧 , 爱情        中国大陆          122分钟      2010-12-22          7.4   \n",
       "1                 爱情        中国香港          117分钟      2009-01-26          6.1   \n",
       "2            爱情 , 剧情   中国台湾,中国大陆          128分钟      2012-02-14          7.8   \n",
       "3                 爱情   中国大陆,中国香港          112分钟      2012-02-09          7.1   \n",
       "4       爱情 , 音乐 , 奇幻   中国香港,中国台湾          101分钟      2007-07-31          8.5   \n",
       "...               ...        ...            ...             ...          ...   \n",
       "15425             NaN       中国香港           13分钟         2015-12          NaN   \n",
       "15426        剧情 , 短片        中国大陆           20分钟      2019-06-25          NaN   \n",
       "15427       纪录片 , 短片        中国大陆            4分钟            2016          NaN   \n",
       "15428             喜剧        中国香港          125分钟      2012-10-11          NaN   \n",
       "15429            纪录片        中国台湾           61分钟      2018-10-25          NaN   \n",
       "\n",
       "       films_box_office  \n",
       "0               47200.0  \n",
       "1               11299.0  \n",
       "2               13700.0  \n",
       "3                2137.0  \n",
       "4                  33.0  \n",
       "...                 ...  \n",
       "15425               NaN  \n",
       "15426               NaN  \n",
       "15427               NaN  \n",
       "15428               NaN  \n",
       "15429               NaN  \n",
       "\n",
       "[15430 rows x 6 columns]"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "my"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'pandas.core.frame.DataFrame'>\n",
      "RangeIndex: 15430 entries, 0 to 15429\n",
      "Data columns (total 6 columns):\n",
      " #   Column            Non-Null Count  Dtype  \n",
      "---  ------            --------------  -----  \n",
      " 0   films_type        13584 non-null  object \n",
      " 1   films_area        15430 non-null  object \n",
      " 2   films_duration    15430 non-null  object \n",
      " 3   film_first_time   15427 non-null  object \n",
      " 4   films_score       2520 non-null   float64\n",
      " 5   films_box_office  1501 non-null   float64\n",
      "dtypes: float64(2), object(4)\n",
      "memory usage: 723.4+ KB\n"
     ]
    }
   ],
   "source": [
    "my.info()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 删掉电影类型为空的行"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "my=my.dropna(subset=['films_type'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'pandas.core.frame.DataFrame'>\n",
      "Int64Index: 13584 entries, 0 to 15429\n",
      "Data columns (total 6 columns):\n",
      " #   Column            Non-Null Count  Dtype  \n",
      "---  ------            --------------  -----  \n",
      " 0   films_type        13584 non-null  object \n",
      " 1   films_area        13584 non-null  object \n",
      " 2   films_duration    13584 non-null  object \n",
      " 3   film_first_time   13581 non-null  object \n",
      " 4   films_score       2510 non-null   float64\n",
      " 5   films_box_office  1501 non-null   float64\n",
      "dtypes: float64(2), object(4)\n",
      "memory usage: 742.9+ KB\n"
     ]
    }
   ],
   "source": [
    "my.info()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>films_type</th>\n",
       "      <th>films_area</th>\n",
       "      <th>films_duration</th>\n",
       "      <th>film_first_time</th>\n",
       "      <th>films_score</th>\n",
       "      <th>films_box_office</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>喜剧 , 爱情</td>\n",
       "      <td>中国大陆</td>\n",
       "      <td>122分钟</td>\n",
       "      <td>2010-12-22</td>\n",
       "      <td>7.4</td>\n",
       "      <td>47200.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>爱情</td>\n",
       "      <td>中国香港</td>\n",
       "      <td>117分钟</td>\n",
       "      <td>2009-01-26</td>\n",
       "      <td>6.1</td>\n",
       "      <td>11299.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>爱情 , 剧情</td>\n",
       "      <td>中国台湾,中国大陆</td>\n",
       "      <td>128分钟</td>\n",
       "      <td>2012-02-14</td>\n",
       "      <td>7.8</td>\n",
       "      <td>13700.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>爱情</td>\n",
       "      <td>中国大陆,中国香港</td>\n",
       "      <td>112分钟</td>\n",
       "      <td>2012-02-09</td>\n",
       "      <td>7.1</td>\n",
       "      <td>2137.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>爱情 , 音乐 , 奇幻</td>\n",
       "      <td>中国香港,中国台湾</td>\n",
       "      <td>101分钟</td>\n",
       "      <td>2007-07-31</td>\n",
       "      <td>8.5</td>\n",
       "      <td>33.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>15423</th>\n",
       "      <td>短片</td>\n",
       "      <td>中国香港</td>\n",
       "      <td>30分钟</td>\n",
       "      <td>2018-05-17</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>15426</th>\n",
       "      <td>剧情 , 短片</td>\n",
       "      <td>中国大陆</td>\n",
       "      <td>20分钟</td>\n",
       "      <td>2019-06-25</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>15427</th>\n",
       "      <td>纪录片 , 短片</td>\n",
       "      <td>中国大陆</td>\n",
       "      <td>4分钟</td>\n",
       "      <td>2016</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>15428</th>\n",
       "      <td>喜剧</td>\n",
       "      <td>中国香港</td>\n",
       "      <td>125分钟</td>\n",
       "      <td>2012-10-11</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>15429</th>\n",
       "      <td>纪录片</td>\n",
       "      <td>中国台湾</td>\n",
       "      <td>61分钟</td>\n",
       "      <td>2018-10-25</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>13584 rows × 6 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "           films_type films_area films_duration film_first_time  films_score  \\\n",
       "0            喜剧 , 爱情        中国大陆          122分钟      2010-12-22          7.4   \n",
       "1                 爱情        中国香港          117分钟      2009-01-26          6.1   \n",
       "2            爱情 , 剧情   中国台湾,中国大陆          128分钟      2012-02-14          7.8   \n",
       "3                 爱情   中国大陆,中国香港          112分钟      2012-02-09          7.1   \n",
       "4       爱情 , 音乐 , 奇幻   中国香港,中国台湾          101分钟      2007-07-31          8.5   \n",
       "...               ...        ...            ...             ...          ...   \n",
       "15423             短片        中国香港           30分钟      2018-05-17          NaN   \n",
       "15426        剧情 , 短片        中国大陆           20分钟      2019-06-25          NaN   \n",
       "15427       纪录片 , 短片        中国大陆            4分钟            2016          NaN   \n",
       "15428             喜剧        中国香港          125分钟      2012-10-11          NaN   \n",
       "15429            纪录片        中国台湾           61分钟      2018-10-25          NaN   \n",
       "\n",
       "       films_box_office  \n",
       "0               47200.0  \n",
       "1               11299.0  \n",
       "2               13700.0  \n",
       "3                2137.0  \n",
       "4                  33.0  \n",
       "...                 ...  \n",
       "15423               NaN  \n",
       "15426               NaN  \n",
       "15427               NaN  \n",
       "15428               NaN  \n",
       "15429               NaN  \n",
       "\n",
       "[13584 rows x 6 columns]"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "my"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 其他特征的处理"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "my['films_type'] = my['films_type'].apply(lambda x: x.split(',')[0])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "def handle_area(x):\n",
    "    area = ['中国大陆','中国香港','中国台湾'] \n",
    "    area_list = x.split(',')\n",
    "    for a in area_list:\n",
    "        if a in area:\n",
    "            return a\n",
    "        else:\n",
    "            continue\n",
    "my['films_area'] = my['films_area'].apply(handle_area)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'pandas.core.frame.DataFrame'>\n",
      "Int64Index: 13584 entries, 0 to 15429\n",
      "Data columns (total 6 columns):\n",
      " #   Column            Non-Null Count  Dtype  \n",
      "---  ------            --------------  -----  \n",
      " 0   films_type        13584 non-null  object \n",
      " 1   films_area        13544 non-null  object \n",
      " 2   films_duration    13584 non-null  object \n",
      " 3   film_first_time   13581 non-null  object \n",
      " 4   films_score       2510 non-null   float64\n",
      " 5   films_box_office  1501 non-null   float64\n",
      "dtypes: float64(2), object(4)\n",
      "memory usage: 742.9+ KB\n"
     ]
    }
   ],
   "source": [
    "my.info()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "my=my.dropna(subset=['films_area'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'pandas.core.frame.DataFrame'>\n",
      "Int64Index: 13544 entries, 0 to 15429\n",
      "Data columns (total 6 columns):\n",
      " #   Column            Non-Null Count  Dtype  \n",
      "---  ------            --------------  -----  \n",
      " 0   films_type        13544 non-null  object \n",
      " 1   films_area        13544 non-null  object \n",
      " 2   films_duration    13544 non-null  object \n",
      " 3   film_first_time   13541 non-null  object \n",
      " 4   films_score       2508 non-null   float64\n",
      " 5   films_box_office  1500 non-null   float64\n",
      "dtypes: float64(2), object(4)\n",
      "memory usage: 740.7+ KB\n"
     ]
    }
   ],
   "source": [
    "my.info()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [],
   "source": [
    "my=my.dropna(subset=['film_first_time'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'pandas.core.frame.DataFrame'>\n",
      "Int64Index: 13541 entries, 0 to 15429\n",
      "Data columns (total 6 columns):\n",
      " #   Column            Non-Null Count  Dtype  \n",
      "---  ------            --------------  -----  \n",
      " 0   films_type        13541 non-null  object \n",
      " 1   films_area        13541 non-null  object \n",
      " 2   films_duration    13541 non-null  object \n",
      " 3   film_first_time   13541 non-null  object \n",
      " 4   films_score       2506 non-null   float64\n",
      " 5   films_box_office  1500 non-null   float64\n",
      "dtypes: float64(2), object(4)\n",
      "memory usage: 740.5+ KB\n"
     ]
    }
   ],
   "source": [
    "my.info()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [],
   "source": [
    "my['films_duration'] = my['films_duration'].apply(lambda x: int(x[:-2]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [],
   "source": [
    "my['film_first_time'] = my['film_first_time'].apply(lambda x: int(x[0:4]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [],
   "source": [
    "my['films_score'] = my['films_score'].apply(lambda x: float(x))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [],
   "source": [
    "my['films_box_office'] = my['films_box_office'].apply(lambda x: float(x))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'pandas.core.frame.DataFrame'>\n",
      "Int64Index: 13541 entries, 0 to 15429\n",
      "Data columns (total 6 columns):\n",
      " #   Column            Non-Null Count  Dtype  \n",
      "---  ------            --------------  -----  \n",
      " 0   films_type        13541 non-null  object \n",
      " 1   films_area        13541 non-null  object \n",
      " 2   films_duration    13541 non-null  int64  \n",
      " 3   film_first_time   13541 non-null  int64  \n",
      " 4   films_score       2506 non-null   float64\n",
      " 5   films_box_office  1500 non-null   float64\n",
      "dtypes: float64(2), int64(2), object(2)\n",
      "memory usage: 740.5+ KB\n"
     ]
    }
   ],
   "source": [
    "my.info()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>films_type</th>\n",
       "      <th>films_area</th>\n",
       "      <th>films_duration</th>\n",
       "      <th>film_first_time</th>\n",
       "      <th>films_score</th>\n",
       "      <th>films_box_office</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>喜剧</td>\n",
       "      <td>中国大陆</td>\n",
       "      <td>122</td>\n",
       "      <td>2010</td>\n",
       "      <td>7.4</td>\n",
       "      <td>47200.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>爱情</td>\n",
       "      <td>中国香港</td>\n",
       "      <td>117</td>\n",
       "      <td>2009</td>\n",
       "      <td>6.1</td>\n",
       "      <td>11299.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>爱情</td>\n",
       "      <td>中国台湾</td>\n",
       "      <td>128</td>\n",
       "      <td>2012</td>\n",
       "      <td>7.8</td>\n",
       "      <td>13700.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>爱情</td>\n",
       "      <td>中国大陆</td>\n",
       "      <td>112</td>\n",
       "      <td>2012</td>\n",
       "      <td>7.1</td>\n",
       "      <td>2137.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>爱情</td>\n",
       "      <td>中国香港</td>\n",
       "      <td>101</td>\n",
       "      <td>2007</td>\n",
       "      <td>8.5</td>\n",
       "      <td>33.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>喜剧</td>\n",
       "      <td>中国大陆</td>\n",
       "      <td>110</td>\n",
       "      <td>2008</td>\n",
       "      <td>5.1</td>\n",
       "      <td>3500.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  films_type films_area  films_duration  film_first_time  films_score  \\\n",
       "0        喜剧        中国大陆             122             2010          7.4   \n",
       "1        爱情        中国香港             117             2009          6.1   \n",
       "2        爱情        中国台湾             128             2012          7.8   \n",
       "3        爱情        中国大陆             112             2012          7.1   \n",
       "4        爱情        中国香港             101             2007          8.5   \n",
       "5        喜剧        中国大陆             110             2008          5.1   \n",
       "\n",
       "   films_box_office  \n",
       "0           47200.0  \n",
       "1           11299.0  \n",
       "2           13700.0  \n",
       "3            2137.0  \n",
       "4              33.0  \n",
       "5            3500.0  "
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "my.head(6)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 特征工程"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 提取特征 独热编码"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [],
   "source": [
    "f_type = pd.get_dummies(my['films_type']) "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th>Sci-Fi</th>\n",
       "      <th>亲情</th>\n",
       "      <th>传记</th>\n",
       "      <th>儿童</th>\n",
       "      <th>公益</th>\n",
       "      <th>公益性</th>\n",
       "      <th>公路喜剧</th>\n",
       "      <th>冒险</th>\n",
       "      <th>军旅</th>\n",
       "      <th>...</th>\n",
       "      <th>警匪</th>\n",
       "      <th>记录片</th>\n",
       "      <th>运动</th>\n",
       "      <th>都市</th>\n",
       "      <th>青春</th>\n",
       "      <th>青春 励志 爱情</th>\n",
       "      <th>青春励志</th>\n",
       "      <th>音乐</th>\n",
       "      <th>魔幻</th>\n",
       "      <th>黑色电影</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>3 rows × 79 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "        Sci-Fi    亲情    传记    儿童    公益    公益性    公路喜剧    冒险    军旅   ...   警匪   \\\n",
       "0   0         0     0     0     0     0      0       0     0     0  ...     0   \n",
       "1   0         0     0     0     0     0      0       0     0     0  ...     0   \n",
       "2   0         0     0     0     0     0      0       0     0     0  ...     0   \n",
       "\n",
       "    记录片    运动    都市    青春    青春 励志 爱情    青春励志    音乐    魔幻    黑色电影   \n",
       "0      0     0     0     0           0       0     0     0       0  \n",
       "1      0     0     0     0           0       0     0     0       0  \n",
       "2      0     0     0     0           0       0     0     0       0  \n",
       "\n",
       "[3 rows x 79 columns]"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "f_type.head(3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [],
   "source": [
    "f_area = pd.get_dummies(my['films_area'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>中国台湾</th>\n",
       "      <th>中国大陆</th>\n",
       "      <th>中国香港</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   中国台湾  中国大陆  中国香港\n",
       "0     0     1     0\n",
       "1     0     0     1\n",
       "2     1     0     0"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "f_area.head(3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [],
   "source": [
    "my = my.join(f_type)\n",
    "my = my.join(f_area)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>films_type</th>\n",
       "      <th>films_area</th>\n",
       "      <th>films_duration</th>\n",
       "      <th>film_first_time</th>\n",
       "      <th>films_score</th>\n",
       "      <th>films_box_office</th>\n",
       "      <th></th>\n",
       "      <th>Sci-Fi</th>\n",
       "      <th>亲情</th>\n",
       "      <th>传记</th>\n",
       "      <th>...</th>\n",
       "      <th>都市</th>\n",
       "      <th>青春</th>\n",
       "      <th>青春 励志 爱情</th>\n",
       "      <th>青春励志</th>\n",
       "      <th>音乐</th>\n",
       "      <th>魔幻</th>\n",
       "      <th>黑色电影</th>\n",
       "      <th>中国台湾</th>\n",
       "      <th>中国大陆</th>\n",
       "      <th>中国香港</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>喜剧</td>\n",
       "      <td>中国大陆</td>\n",
       "      <td>122</td>\n",
       "      <td>2010</td>\n",
       "      <td>7.4</td>\n",
       "      <td>47200.0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>爱情</td>\n",
       "      <td>中国香港</td>\n",
       "      <td>117</td>\n",
       "      <td>2009</td>\n",
       "      <td>6.1</td>\n",
       "      <td>11299.0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>爱情</td>\n",
       "      <td>中国台湾</td>\n",
       "      <td>128</td>\n",
       "      <td>2012</td>\n",
       "      <td>7.8</td>\n",
       "      <td>13700.0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>爱情</td>\n",
       "      <td>中国大陆</td>\n",
       "      <td>112</td>\n",
       "      <td>2012</td>\n",
       "      <td>7.1</td>\n",
       "      <td>2137.0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>爱情</td>\n",
       "      <td>中国香港</td>\n",
       "      <td>101</td>\n",
       "      <td>2007</td>\n",
       "      <td>8.5</td>\n",
       "      <td>33.0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 88 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "  films_type films_area  films_duration  film_first_time  films_score  \\\n",
       "0        喜剧        中国大陆             122             2010          7.4   \n",
       "1        爱情        中国香港             117             2009          6.1   \n",
       "2        爱情        中国台湾             128             2012          7.8   \n",
       "3        爱情        中国大陆             112             2012          7.1   \n",
       "4        爱情        中国香港             101             2007          8.5   \n",
       "\n",
       "   films_box_office       Sci-Fi    亲情    传记   ...   都市    青春    青春 励志 爱情   \\\n",
       "0           47200.0   0         0     0     0  ...     0     0           0   \n",
       "1           11299.0   0         0     0     0  ...     0     0           0   \n",
       "2           13700.0   0         0     0     0  ...     0     0           0   \n",
       "3            2137.0   0         0     0     0  ...     0     0           0   \n",
       "4              33.0   0         0     0     0  ...     0     0           0   \n",
       "\n",
       "    青春励志    音乐    魔幻    黑色电影   中国台湾  中国大陆  中国香港  \n",
       "0       0     0     0       0     0     1     0  \n",
       "1       0     0     0       0     0     0     1  \n",
       "2       0     0     0       0     1     0     0  \n",
       "3       0     0     0       0     0     1     0  \n",
       "4       0     0     0       0     0     0     1  \n",
       "\n",
       "[5 rows x 88 columns]"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "my.head(5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [],
   "source": [
    "my = my.drop(['films_type','films_area'],axis=1)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 缺失值填充 中位数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 中位数\n",
    "f_score_median = my['films_score'].median()\n",
    "f_office_median = my['films_box_office'].median()\n",
    "my['films_score'] =my['films_score'].fillna(f_score_median)\n",
    "my['films_box_office'] = my['films_box_office'].fillna(f_office_median)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 数据集选择"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [],
   "source": [
    "data = my.loc[:,my.columns != 'films_box_office'].values\n",
    "label = my.loc[:,my.columns == 'films_box_office'].values"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 122. , 2010. ,    7.4, ...,    0. ,    1. ,    0. ],\n",
       "       [ 117. , 2009. ,    6.1, ...,    0. ,    0. ,    1. ],\n",
       "       [ 128. , 2012. ,    7.8, ...,    1. ,    0. ,    0. ],\n",
       "       ...,\n",
       "       [   4. , 2016. ,    6.9, ...,    0. ,    1. ,    0. ],\n",
       "       [ 125. , 2012. ,    6.9, ...,    0. ,    0. ,    1. ],\n",
       "       [  61. , 2018. ,    6.9, ...,    1. ,    0. ,    0. ]])"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.set_printoptions(suppress=True)\n",
    "data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([47200. , 11299. , 13700. , ...,   315.5,   315.5,   315.5])"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "label = label.flatten()\n",
    "label"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 特征选择"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 卡方检验"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[7.4 0.  0.  ... 0.  1.  0. ]\n",
      " [6.1 0.  0.  ... 0.  0.  1. ]\n",
      " [7.8 0.  0.  ... 1.  0.  0. ]\n",
      " ...\n",
      " [6.9 0.  0.  ... 0.  1.  0. ]\n",
      " [6.9 0.  0.  ... 0.  0.  1. ]\n",
      " [6.9 0.  0.  ... 1.  0.  0. ]]\n"
     ]
    }
   ],
   "source": [
    "from sklearn.feature_selection import RFE\n",
    "from sklearn.linear_model import LogisticRegression\n",
    "\n",
    "#递归特征消除法，返回特征选择后的数据\n",
    "#参数estimator为基模型\n",
    "#参数n_features_to_select为选择的特征个数\n",
    "featureArr = RFE(estimator=LogisticRegression(), n_features_to_select=12).fit_transform(data, label.astype('int'))\n",
    "print(featureArr)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 相关系数法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.feature_selection import SelectKBest\n",
    "from scipy.stats import pearsonr\n",
    "\n",
    "#选择K个最好的特征，返回选择特征后的数据\n",
    "#第一个参数为计算评估特征是否好的函数，该函数输入特征矩阵和目标向量，输出二元组（评分，P值）的数组，数组第i项为第i个特征的评分和P值。在此定义为计算相关系数\n",
    "#参数k为选择的特征个数\n",
    "SelectKBest(lambda X, Y: array(map(lambda x:pearsonr(x, Y), X.T)).T, k=2).fit_transform(iris.data, iris.target)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 递归特征消除法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.feature_selection import RFE\n",
    "from sklearn.linear_model import LogisticRegression\n",
    "\n",
    "#递归特征消除法，返回特征选择后的数据\n",
    "#参数estimator为基模型\n",
    "#参数n_features_to_select为选择的特征个数\n",
    "RFE(estimator=LogisticRegression(), n_features_to_select=12).fit_transform(data,label.astype('int'))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 降维"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 主成分分析法 PCA"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[-0.54859975 -0.16226606  0.47512136  0.41921462 -0.45826603  0.70990641]\n",
      " [ 1.07734475 -0.54195081 -0.12160843  0.76684985  0.15229722 -0.36925951]\n",
      " [-0.59681374 -1.13547827 -0.51038282 -0.13278182 -0.68448688 -0.21925912]\n",
      " ...\n",
      " [-0.1574833  -0.31054972  0.67203101 -0.36864163  0.53248026  0.05577262]\n",
      " [ 0.36854882 -0.77801883 -0.19601553  1.04879942 -0.06069364  0.64721473]\n",
      " [ 0.20786794 -1.19477193 -0.17500101 -0.80183348  0.1176929   0.11887261]]\n"
     ]
    }
   ],
   "source": [
    "from sklearn.decomposition import PCA\n",
    "#主成分分析法，返回降维后的数据\n",
    "#参数n_components为主成分数目\n",
    "data = PCA(n_components=6).fit_transform(featureArr)\n",
    "print(data)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 线性判别分析法 LDA"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.lda import LDA \n",
    "#线性判别分析法，返回降维后的数据\n",
    "#参数n_components为降维后的维数\n",
    "data = LDA(n_components=6).fit_transform(data,label)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 机器学习"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.ensemble import RandomForestRegressor\n",
    "from sklearn.model_selection import GridSearchCV\n",
    "from sklearn.pipeline import Pipeline\n",
    "from sklearn.ensemble import AdaBoostRegressor # AdaBoost回归器"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "metadata": {},
   "outputs": [],
   "source": [
    "rf = RandomForestRegressor()\n",
    "parameters = {\"randomforestregressor__n_estimators\": range(20,25)}\n",
    "pipeline = Pipeline([('randomforestregressor', rf)])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {},
   "outputs": [],
   "source": [
    "clf = GridSearchCV(estimator=pipeline, param_grid=parameters)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "GridSearchCV(cv='warn', error_score='raise-deprecating',\n",
       "             estimator=Pipeline(memory=None,\n",
       "                                steps=[('randomforestregressor',\n",
       "                                        RandomForestRegressor(bootstrap=True,\n",
       "                                                              criterion='mse',\n",
       "                                                              max_depth=None,\n",
       "                                                              max_features='auto',\n",
       "                                                              max_leaf_nodes=None,\n",
       "                                                              min_impurity_decrease=0.0,\n",
       "                                                              min_impurity_split=None,\n",
       "                                                              min_samples_leaf=1,\n",
       "                                                              min_samples_split=2,\n",
       "                                                              min_weight_fraction_leaf=0.0,\n",
       "                                                              n_estimators='warn',\n",
       "                                                              n_jobs=None,\n",
       "                                                              oob_score=False,\n",
       "                                                              random_state=None,\n",
       "                                                              verbose=0,\n",
       "                                                              warm_start=False))],\n",
       "                                verbose=False),\n",
       "             iid='warn', n_jobs=None,\n",
       "             param_grid={'randomforestregressor__n_estimators': range(20, 25)},\n",
       "             pre_dispatch='2*n_jobs', refit=True, return_train_score=False,\n",
       "             scoring=None, verbose=0)"
      ]
     },
     "execution_count": 47,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "clf.fit(featureArr,label)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "最优分数： 0.1156\n",
      "最优参数： {'randomforestregressor__n_estimators': 20}\n"
     ]
    }
   ],
   "source": [
    "print(\"最优分数： %.4lf\" %clf.best_score_)\n",
    "print(\"最优参数：\", clf.best_params_)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "metadata": {},
   "outputs": [],
   "source": [
    "reg = AdaBoostRegressor()\n",
    "parameters = {\"adaboostregressor__n_estimators\": range(10,30)}\n",
    "pipeline = Pipeline([('adaboostregressor', reg)])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "metadata": {},
   "outputs": [],
   "source": [
    "clf_ada = GridSearchCV(estimator=pipeline, param_grid=parameters)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "GridSearchCV(cv='warn', error_score='raise-deprecating',\n",
       "             estimator=Pipeline(memory=None,\n",
       "                                steps=[('adaboostregressor',\n",
       "                                        AdaBoostRegressor(base_estimator=None,\n",
       "                                                          learning_rate=1.0,\n",
       "                                                          loss='linear',\n",
       "                                                          n_estimators=50,\n",
       "                                                          random_state=None))],\n",
       "                                verbose=False),\n",
       "             iid='warn', n_jobs=None,\n",
       "             param_grid={'adaboostregressor__n_estimators': range(10, 30)},\n",
       "             pre_dispatch='2*n_jobs', refit=True, return_train_score=False,\n",
       "             scoring=None, verbose=0)"
      ]
     },
     "execution_count": 51,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "clf_ada.fit(featureArr,label)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "最优分数： -0.5895\n",
      "最优参数： {'adaboostregressor__n_estimators': 15}\n"
     ]
    }
   ],
   "source": [
    "print(\"最优分数： %.4lf\" %clf_ada.best_score_)\n",
    "print(\"最优参数：\", clf_ada.best_params_)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 选用了4种回归模型，最小二乘法 、 岭回归、索套、随机森林\n",
    "from sklearn import model_selection\n",
    "from sklearn.ensemble import RandomForestRegressor as RFR # 随机森林\n",
    "from sklearn.linear_model import LinearRegression as LR # 最小二乘法\n",
    "from sklearn.linear_model import Ridge as R # 岭回归\n",
    "from sklearn.linear_model import Lasso as L # 索套\n",
    "import warnings\n",
    "warnings.filterwarnings(\"ignore\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "metadata": {},
   "outputs": [],
   "source": [
    "def predict_score(data,label,model):\n",
    "    i = np.random.permutation(len(data))\n",
    "    x_train = data[i[:-3584]]\n",
    "    y_train = label[i[:-3584]]\n",
    "    x_test = data[i[-3584:]]\n",
    "    y_test = label[i[-3584:]]\n",
    "    Model = model()\n",
    "    Model.fit(x_train,y_train)\n",
    "    score = Model.score(x_test,y_test)\n",
    "    return score"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'sklearn.ensemble.forest.RandomForestRegressor'>回归模型的准确率为0.12966346592077282\n",
      "<class 'sklearn.linear_model.base.LinearRegression'>回归模型的准确率为-0.004354904805465409\n",
      "<class 'sklearn.linear_model.ridge.Ridge'>回归模型的准确率为0.05227504102204439\n",
      "<class 'sklearn.linear_model.coordinate_descent.Lasso'>回归模型的准确率为0.03746306517716158\n"
     ]
    }
   ],
   "source": [
    "modellist = [RFR,LR,R,L]\n",
    "for m in modellist:\n",
    "    model = m\n",
    "    score = predict_score(featureArr,label,model)\n",
    "    print(str(model)+'回归模型的准确率为'+str(score))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
